/*
 *  Copyright (C) 2011 glauco
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.balisunrise.samples;

import java.util.ArrayList;
import java.util.List;
import org.balisunrise.annotation.ActionMap;
import org.balisunrise.annotation.VisualAction;
import org.balisunrise.annotation.VisualActionTarget;
import org.balisunrise.common.Basic;
import org.balisunrise.common.EntityField;
import org.balisunrise.common.Event;
import org.balisunrise.common.Messenger;
import org.balisunrise.common.Search;
import org.balisunrise.common.VisualEntity;
import org.balisunrise.common.defaults.VEAUtil;
import org.balisunrise.swing.common.Icons;

/**
 * Mapa de ações de exemplos.
 *
 * @author glauco
 */
@ActionMap(classes=Estado.class)
public class ActionSample 
        implements org.balisunrise.common.ActionMap<VisualEntity, EntityField>{

    private List<VisualEntity> veList = new ArrayList<VisualEntity>();
    private List<EntityField> efList = new ArrayList<EntityField>();

    @VisualAction(target=VisualActionTarget.SEARCH,
        icon=Icons.MONEY1,
        index=0,
        text="Exemplo",
        toolTipText="Ação de Exemplo")
    public void sample(Event evt){

        Basic b;
        if (veList.size() > 0)
            b = veList.get(0);
        else
            b = evt.getCreator();

        Messenger m = VEAUtil.getMessenger(b);

        b = VEAUtil.getFromBasic(b, Search.class);
        if(b != null){
            Search s = (Search) b;
            Object entity = s.getSelectedEntity();
            if(entity != null){
                String msg = "Entidade Selecionada: " + entity.toString();
                m.notify(msg, m.INFORMATION);
            }
        }
    }

    @Override
    public void addVisualEntity(VisualEntity v) {
        if(v == null || veList.contains(v))
            return ;
        veList.add(v);
    }

    @Override
    public void addEntityField(EntityField f) {
        if(f == null || efList.contains(f))
            return ;
        efList.add(f);
    }
}
